package com.example.ai.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

// 参考 Spring AI 官方文档 https://docs.spring.io/spring-ai/reference/api/chat/deepseek-chat.html#_sample_controller
@RestController
@RequestMapping("/ai/chat")
public class ChatController {

    @Resource
    private OpenAiChatModel openAiChatModel;

    @GetMapping("/generate")
    public String generate(@RequestParam(value = "message", defaultValue = "你好，能不能给我讲一个笑话") String message) {
        return openAiChatModel.call(message);
    }

    @GetMapping("/generate-stream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "你好，能不能给我讲一个笑话") String message) {
//        PromptTemplate promptTemplate = new PromptTemplate("Tell me a joke about {subject}");
//        Prompt prompt = promptTemplate.create(Map.of("subject", subject));

        Prompt prompt1 = new Prompt(new UserMessage(message));
        return openAiChatModel.stream(prompt1);
    }
}
